package com.wacai.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wacai.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags = "User")
@RestController
@RequestMapping(value = "/users")
public class APIController {

	private static Logger LOGGER = LoggerFactory.getLogger(APIController.class);
	
	private List<User> userList = new ArrayList<>();
	
	{
		userList.addAll(Arrays.asList(new User(1,"罗汉果1",""), new User(2,"罗汉果2","")));
	}
	
	@ApiOperation(value = "获取用户列表", notes = "", produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET")
	@ApiResponses({
		@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = User[].class)
	})
	@GetMapping(value="/", produces = {"application/json"})
	public List<User> getUserList() {
		LOGGER.debug("----getUserList------");
		return userList;
	}
	
	@ApiOperation(value = "创建用户", notes = "根据user对象创建用户", httpMethod = "POST")
	@ApiImplicitParam(name = "user", value = "用户详细实体User", required = true, paramType = "body", dataType = "User")
	@PostMapping("/")
	public String postUser(@RequestBody User user) {
		LOGGER.debug("-----postUser-------");
		this.userList.add(user);
		return "success";
	}
	
	@ApiOperation(value = "获取用户详细信息", notes = "根据path中的id来获取用户详细信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Integer")
	@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = User.class)
	@GetMapping(value = "/{id}")
	public User getUser(@PathVariable("id") Integer id) {
		LOGGER.debug("-------GetUserById {}-----", id);
		return userList.stream().filter(e -> e.getId().equals(id)).findFirst().get();
	}
	
	@ApiOperation(value = "更新用户信息", notes = "根据url的id来指定更新对象，并根据RequestBody中的user来更新用户详细信息")
	@ApiResponse(code = 200, message = "ok", response = String.class)
	@PatchMapping(value = "/{id}")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Integer"),
		@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, paramType = "body", dataType = "User")
	})
	public String patchUser(@PathVariable Integer id, @RequestBody User user) {
		LOGGER.debug("-----patchUser------");
		User u = userList.stream().filter(e -> e.getId().equals(id)).findFirst().get();
		u.setName(user.getName());
		u.setDesc(user.getDesc());
		return "success";
	}
	
	@ApiOperation(value = "删除用户", notes = "根据url的id来指定删除的对象")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Integer")
	@ApiResponse(code = HttpStatus.SC_OK, message = "success", response = String.class)
	@DeleteMapping(value = "/{id}")
	public String deleteUser(@PathVariable Integer id) {
		LOGGER.debug("------deleteUser----id: {}---", id);
		User u = userList.stream().filter(e -> e.getId().equals(id)).findFirst().get();
		userList.remove(u);
		return "success";
	}
}
